//
//  FriendViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/15.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "FriendViewController.h"
#import "UnRealNameViewController.h"
#import "HadRealNameViewController.h"
#import "SMPagerTabView.h"
#import "UISearchBar+JCSearchBarPlaceholder.h"
#import "FriendList.h"

@interface FriendViewController ()<SMPagerTabViewDelegate,UISearchBarDelegate>

@property (nonatomic, strong) NSMutableArray *vcArr;
@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic, strong) SMPagerTabView *segmentView;

@end

@implementation FriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self setUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableViewDidScroll) name:@"DidScroll" object:nil];
}

- (void)setupNavBar
{
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0,0,ScreenWidth-100,35)];
    self.searchBar = [[UISearchBar alloc] initWithFrame:titleView.frame];
    self.searchBar.placeholder = @" 姓名";
    //[self.searchBar changeLeftPlaceholder:@" 姓名"];
    self.searchBar.layer.cornerRadius = 15;
    self.searchBar.layer.masksToBounds = YES;
    //设置背景图是为了去掉上下黑线
    self.searchBar.backgroundImage = [[UIImage alloc] init];
    self.searchBar.backgroundColor = [UIColor whiteColor];
    self.searchBar.delegate = self;
    //5. 设置搜索Icon
    [self.searchBar setImage:[UIImage imageNamed:@"Search_Icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    //一下代码为修改placeholder字体的颜色和大小
    UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
    //只有编辑时出现出现那个叉
    searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
    searchField.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:14];
    [titleView addSubview:self.searchBar];
    
    //Set to titleView
    self.navigationItem.titleView = titleView;
    
}

- (void)setUI{
    
    _vcArr = [NSMutableArray array];
    
    HadRealNameViewController *vc1 = [[HadRealNameViewController alloc]initWithNibName:nil bundle:nil];
    vc1.title = @"已实名";
    
    UnRealNameViewController *vc2 = [[UnRealNameViewController alloc]initWithNibName:nil bundle:nil];
    vc2.title = @"未实名";
    
    [_vcArr addObject:vc1];
    [_vcArr addObject:vc2];
    
    self.segmentView.delegate = self;
    
    //可自定义背景色和tab button的文字颜色等
    //开始构建UI
    [_segmentView buildUI];
    
    //起始选择一个tab
    [_segmentView selectTabWithIndex:0 animate:NO];
    
    [FriendList sharedInstance].ifHadRealNameNoti = @"HadRealName";
    
}

#pragma mark - DBPagerTabView Delegate
- (NSUInteger)numberOfPagers:(SMPagerTabView *)view {
    return [_vcArr count];
}
- (UIViewController *)pagerViewOfPagers:(SMPagerTabView *)view indexOfPagers:(NSUInteger)number {
    return _vcArr[number];
}

- (void)whenSelectOnPager:(NSUInteger)number {
    NSLog(@"页面 %lu",(unsigned long)number);
    
    [FriendList sharedInstance].flag = number;
    
    if (number == 0) {
        
        [FriendList sharedInstance].ifHadRealNameNoti = @"HadRealName";
    }else{
        
        [FriendList sharedInstance].ifHadRealNameNoti = @"UnRealName";
    }
}

#pragma mark - setter/getter
- (SMPagerTabView *)segmentView {
    if (!_segmentView) {
        self.segmentView = [[SMPagerTabView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - (iPhoneX_Top))];
        [self.view addSubview:_segmentView];
    }
    return _segmentView;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    [searchBar resignFirstResponder];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    
    NSLog(@"检索词：%@",searchBar.text);
    
    NSString *str = searchBar.text;
    if (searchBar.text.length <= 0) {
        
        str = @"no search";
    }
    
    NSDictionary *dic = @{
                          @"searText":str
                          };
    [[NSNotificationCenter defaultCenter] postNotificationName:[FriendList sharedInstance].ifHadRealNameNoti object:nil userInfo:dic];
}

- (void)tableViewDidScroll{
    
    [self.searchBar resignFirstResponder];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
